package com.yuan.zs.ui.wonderful;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;

import com.yuan.base.utils.UIUtil;
import com.yuan.zs.R;

/**
 * 评论弹出框
 */
class CommentDialog extends AlertDialog {

	private View v_shadow;
	private EditText et_comment;
	private Button btn_submit;
	private int pushId;
	private OnClick onClick;

	CommentDialog(@NonNull Context context, int pushId, OnClick onClick) {
		super(context, R.style.CustDialog);
		this.pushId = pushId;
		this.onClick = onClick;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_comment);
		getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
		bindViews();
	}

	private void bindViews() {
		v_shadow = findViewById(R.id.v_shadow);
		et_comment = findViewById(R.id.et_comment);
		btn_submit = findViewById(R.id.btn_submit);
		v_shadow.setOnClickListener(v -> {
			UIUtil.hidePan(getContext(), et_comment);
			cancel();
		});
		btn_submit.setOnClickListener(v -> onClick.onSubmit(et_comment.getText().toString(), this));
	}

	String getText() {
		return et_comment.getText().toString();
	}

	@Override
	public void show() {

//		Window dialogWindow = getWindow();
//		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
//		DisplayMetrics d = getContext().getResources().getDisplayMetrics(); // 获取屏幕宽、高用
//		lp.width = d.widthPixels; // 高度设置为屏幕的0.6
//		lp.height = d.heightPixels; // 高度设置为屏幕的0.6
//		dialogWindow.setAttributes(lp);
//		UIUtil.showPan(et_comment);
		setView(new EditText(getContext()));
		super.show();
		resetScreen();
		new Handler().postDelayed(() -> {
			//设置可获得焦点
			et_comment.setFocusable(true);
			et_comment.setFocusableInTouchMode(true);
			//请求获得焦点
			et_comment.requestFocus();
			InputMethodManager inputManager = (InputMethodManager) et_comment.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
			inputManager.showSoftInput(et_comment, 0);
		}, 200);

	}

	private void resetScreen() {
		Window win = getWindow();
		win.getDecorView().setPadding(0, 0, 0, 0);
		WindowManager.LayoutParams lp = win.getAttributes();
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;
		lp.height = WindowManager.LayoutParams.MATCH_PARENT;
		win.setAttributes(lp);
		if (Build.VERSION.SDK_INT >= 19) {
			getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		}

	}

	public interface OnClick {
		void onSubmit(String content, Dialog dialog);
	}

}
